import HttpAPI from "../../HttpManager/HttpAPI";
import HttpManager from "../../HttpManager/HttpManager";
import { DailyDetailsItemInfo } from "../Data/DailyDetailsItemInfo";
import { EventNames } from "../Data/EventNames";
import { DailyItem } from "./DailyItem";
import { ViewDailyGameRunBase } from "./ViewDailyGameRun.generated";

const { regClass, property } = Laya;

@regClass()
export class ViewDailyGameRun extends ViewDailyGameRunBase {

    /**日报详情列表信息 */
    private arrDailyDetailsItemInfo: DailyDetailsItemInfo[] = [];
    onAwake(): void {
        this.init();
        this.addEventListener();
    }

    onDestroy(): void {
        this.removeEventListener();
    }

    private addEventListener(): void {
        this.btnClose.on(Laya.Event.CLICK, this, this.onClickCloseBtn);
        Laya.stage.on(EventNames.DAILY_DETAILS_SHOW, this, this.onShowDailyDetails);
        Laya.stage.on(EventNames.DAILY_DETAILS_HIDE, this, this.onHideDailyDetails);
    }

    private removeEventListener(): void {
        this.btnClose.off(Laya.Event.CLICK, this, this.onClickCloseBtn);
        Laya.stage.off(EventNames.DAILY_DETAILS_SHOW, this, this.onShowDailyDetails);
        Laya.stage.off(EventNames.DAILY_DETAILS_HIDE, this, this.onHideDailyDetails);
    }

    public showView(): void {
        HttpManager.get(HttpAPI.GET_DAILY_LIST, null, null).then(this.onGetDailyList.bind(this)).catch(this.onGetDailyList.bind(this));
        // this.arrDailyDetailsItemInfo = [];
        // for (let i = 0; i < 10; i++) {
        //     let idid = i + 1;
        //     let data = {
        //         id: idid,
        //         title: `日报${idid}`,
        //         createTime: "2025-04-01",
        //         content: `内容${idid}`,
        //         imgUrlList: ["resources/layaAir.png"],
        //     }
        //     for (let index = 0; index < idid; index++) {
        //         data.imgUrlList.push("resources/layaAir.png");
        //     }
        //     let info: DailyDetailsItemInfo = new DailyDetailsItemInfo(data);
        //     this.arrDailyDetailsItemInfo.push(info);
        // }

        // if (this.arrDailyDetailsItemInfo.length == 0) {
        //     this.noneDailyState.visible = true;
        //     this.DailyList.visible = false;
        // } else {
        //     this.noneDailyState.visible = false;
        //     this.DailyList.visible = true;
        //     this.updateDailyItemList();
        // }
        // this.visible = true;
    }
    private onGetDailyList(data: any) {
        this.arrDailyDetailsItemInfo = [];
        if (data && data.success) {
            const result = data.result;
            for (let item of result) {
                let info: DailyDetailsItemInfo = new DailyDetailsItemInfo(item);
                this.arrDailyDetailsItemInfo.push(info);
            }
        }
        if (this.arrDailyDetailsItemInfo.length == 0) {
            this.noneDailyState.visible = true;
            this.DailyList.visible = false;
        } else {
            this.noneDailyState.visible = false;
            this.DailyList.visible = true;
            this.updateDailyItemList();
        }
        this.visible = true;
    }

    private init() {
        this.viewDailyPanel.visible = true;
        this.viewDailyDetailsPanel.visible = false;
        this.initDailyDetailsItemListData();
    }

    private onClickCloseBtn(): void {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        this.visible = false;
    }

    /**创建list列表 */
    private initDailyDetailsItemListData(): void {
        this.DailyList.renderHandler = Laya.Handler.create(this, this.onListRender, undefined, false);
        this.DailyList.array = this.arrDailyDetailsItemInfo;
    }

    /**监听列表滑动，刷新列表里的数据 */
    private onListRender(item: Laya.Box, index: number) {
        let info: DailyDetailsItemInfo = this.arrDailyDetailsItemInfo[index];
        item.getComponent(DailyItem).updateInfo(info);
    }
    /**刷新日历列表 */
    private updateDailyItemList(): void {
        this.DailyList.array = this.arrDailyDetailsItemInfo;
        this.DailyList.repeatY = this.arrDailyDetailsItemInfo.length;
        this.DailyList.refresh();
        this.DailyList.scrollTo(0);
    }
    /**监听显示日历详情页面 */
    private onShowDailyDetails(info: DailyDetailsItemInfo): void {
        this.viewDailyPanel.visible = false;
        this.viewDailyDetailsPanel.showView(info);
    }
    /**隐藏日报详情 */
    private onHideDailyDetails(dailyId: number): void {
        this.viewDailyPanel.visible = true;
    }
}